package com.input4hua.expr.time.handler.day;

import static com.input4hua.expr.time.DataHandlerConstants.MINUS;
import static com.input4hua.expr.time.DataHandlerConstants.PLUS;

import com.input4hua.expr.time.AbstractDateHandler;
import com.input4hua.expr.time.untils.TimeCalcUtils;
import com.input4hua.expr.time.untils.TimeFormatterUtils;

/**
 * 支持的 ${TODAY}  极其相对偏移日期  输出yyyyMMdd
 * @author Jiahua.hu
 * @since 13 May 2019
 */
public abstract class AbstractDayExprHandler extends AbstractDateHandler {

	protected String exprParse(String expr, String timeFormat) {
		String resp;
		if (expr.contains(PLUS)) {
			String subStr = expr.substring(expr.indexOf(PLUS) + 1, expr.length() - 1);
			resp = TimeFormatterUtils.Date2String(TimeCalcUtils.nextDays(Integer.valueOf(subStr)), timeFormat);
		}
		else if (expr.contains(MINUS)) {
			String subStr = expr.substring(expr.indexOf(MINUS) + 1, expr.length() - 1);
			resp = TimeFormatterUtils.Date2String(TimeCalcUtils.prevDays(Integer.valueOf(subStr)), timeFormat);
		}
		else {
			resp = TimeFormatterUtils.Date2String(TimeCalcUtils.now(), timeFormat);
		}
		return resp;
	}
}
